package com.bestcem.xm.user.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.enums.ReqErrorCodeEnum;
import com.bestcem.xm.common.core.enums.ResourceErrorCodeEnum;
import com.bestcem.xm.common.core.uitls.StringUtil;
import com.bestcem.xm.user.dao.UserBookingTrackDao;
import com.bestcem.xm.user.entity.pojo.UserBookingTrackDO;
import com.bestcem.xm.user.service.UserBookingTrackService;
import com.bestcem.xm.user.service.dto.user.UserBookingTrackDTO;
import com.bestcem.xm.user.util.convert.UserBookingTrackConvert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户埋点数据服务
 *
 * @author qiongyu.cui <qiongyu.cui@idiaoyan.com>
 * @version v1.0
 * @date 2022/3/17 16:50
 */
@Service
public class UserBookingTrackServiceImpl implements UserBookingTrackService {

    @Resource
    private UserBookingTrackDao userBookingTrackDao;

    @Resource
    private UserBookingTrackConvert userBookingTrackConvert;

    @Override
    public ServiceResult<UserBookingTrackDTO> selectById(String bookingTrackId) {
        if (StringUtils.isBlank(bookingTrackId)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "用户埋点记录id不能为空！");
        }
        UserBookingTrackDO userBookingTrackDO = userBookingTrackDao.selectById(bookingTrackId);
        if (Objects.isNull(userBookingTrackDO)) {
            return ServiceResult.fail(ResourceErrorCodeEnum.NOT_FOUND, "未查询到对应的用户埋点信息");
        }
        return ServiceResult.success(userBookingTrackConvert.do2Dto(userBookingTrackDO));
    }

    @Override
    public ServiceResult<List<UserBookingTrackDTO>> selectBySelective(UserBookingTrackDTO userBookingTrackDTO) {
        List<UserBookingTrackDO> userBookingTrackDos = userBookingTrackDao.selectBySelective(
                userBookingTrackConvert.dto2Do(userBookingTrackDTO));
        return ServiceResult.success(userBookingTrackConvert.dos2Dtos(userBookingTrackDos));
    }

    @Override
    public ServiceResult<String> insertSelective(UserBookingTrackDTO userBookingTrackDTO) {
        if (Objects.isNull(userBookingTrackDTO)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "参数信息不能为空");
        }
        userBookingTrackDTO.setBookingtrackId(StringUtil.getId());
        String bookingTrackId = userBookingTrackDao.insertSelective(userBookingTrackConvert.dto2Do(userBookingTrackDTO));
        return ServiceResult.success(bookingTrackId);
    }

    @Override
    public ServiceResult<Integer> updateSelectiveById(UserBookingTrackDTO userBookingTrackDTO) {
        if (Objects.isNull(userBookingTrackDTO)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "参数信息不能为空");
        }
        Integer result = userBookingTrackDao.updateSelectiveById(userBookingTrackConvert.dto2Do(userBookingTrackDTO));
        return ServiceResult.success(result);
    }

    @Override
    public ServiceResult<Integer> updateSelectiveByOrgCode(UserBookingTrackDTO userBookingTrackDTO) {
        if (Objects.isNull(userBookingTrackDTO)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "参数信息不能为空");
        }
        Integer result = userBookingTrackDao.updateSelectiveByOrgCode(userBookingTrackConvert.dto2Do(userBookingTrackDTO));
        return ServiceResult.success(result);
    }

    @Override
    public ServiceResult<String> insert(UserBookingTrackDTO userBookingTrackDTO) {
        if (Objects.isNull(userBookingTrackDTO)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "参数信息不能为空");
        }
        String bookingTrackId = userBookingTrackDao.insert(userBookingTrackConvert.dto2Do(userBookingTrackDTO));
        return ServiceResult.success(bookingTrackId);
    }

    @Override
    public ServiceResult<List<UserBookingTrackDTO>> findAndGenFileByCreateTimeInterval(Date startTime, Date endTime) {
        if (Objects.isNull(startTime) || Objects.isNull(endTime) || startTime.after(endTime)) {
            return ServiceResult.fail(ReqErrorCodeEnum.PARAM_LOST, "时间区间不正确");
        }

        // 根据时间区间查询埋点信息
        List<UserBookingTrackDO> bookingTracks = userBookingTrackDao.selectByCreateTimeInterval(startTime, endTime);
        return ServiceResult.success(userBookingTrackConvert.dos2Dtos(bookingTracks));
    }

}
